﻿

/// <reference path='ol3/ol-whitespace.js' />
/// <reference path='LiuGsource.js' />
/// <reference path='LiuGLayerManagement.js' />


window.onload = Init;

function Init()
{
    var view2= new ol.View2D
                ({
                    center: [-0, 0],
                    zoom: 1
                });

    var map = new ol.Map
        ({
            
            target: 'map',
            controls:
                [
                    new ol.control.MousePosition(),
                    new ol.control.ZoomSlider(),
                    new ol.control.ScaleLine(),
                    new ol.control.Zoom(),
                    //new ol.control.LiuGLayerManagement(),
                    //new ol.control.LiuGForm(),
                    new ol.control.LiuGNavigation()
                ],
            view: view2,
            //renderer:'dom'
        });

    //var layer1 = new ol.layer.Tile
    //    ({
    //        source: new ol.source.OSM
    //            ({
    //                url: 'http://{a-c}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png'
    //            }),
    //        layerName:'OSM地图数据'
    //    });

    //map.addLayer(layer1);



    var layer2 = new ol.layer.Tile
        ({
            source: new ol.source.LiuG
                ({
                    url: 'http://localhost:6666/geowebcache/service/gmaps',
                    params:
                        {
                            'layers': 'globe6',
                            'format': 'image/png',
                        },
                    maxZoom: 22,
                }),
            layerName: '全球谷歌数据前6级'
        });
    map.addLayer(layer2);


    //var bingmap = new ol.layer.Tile({
    //    preload: Infinity,
    //    source: new ol.source.BingMaps({
    //        key: 'Ak-dzM4wZjSqTlzveKz5u0d4IQ4bRzVI309GxmkgSVr1ewS6iPSrOvOKhA-CJlm3',
    //        imagerySet: 'AerialWithLabels'
    //    })
    //});

    //map.addLayer(bingmap);

    var debugLayer = new ol.layer.Tile
        ({
            source: new ol.source.TileDebug
                ({
                    projection: 'EPSG:3857',
                    tileGrid: new ol.tilegrid.XYZ
                        ({
                            maxZoom: 22
                        })
                }),
            layerName:'测试图层'
        });
    //map.addLayer(debugLayer);

    //var layerGroup = new ol.layer.Group({
    //    layers: [layer2,debugLayer],
    //    layerName: '图层组测试'
    //});

    //map.addLayer(layerGroup);


    var getFeatureSource = new ol.source.LiuGTileWMS
                ({
                    url: 'http://localhost:6666/geoserver/LiuG/wms',
                    params:
                        {
                            'LAYERS': 'bou2',
                        },
                });

    var layerGetFeature = new ol.layer.Tile
        ({
            source: getFeatureSource,
            layerName: '中国行政区划'
        });

    //map.addLayer(layerGetFeature);

    


    var getFeatureSource2 = new ol.source.TileWMS
                ({
                    url: 'http://localhost:6666/geoserver/LiuG/wms',
                    params:
                        {
                            'LAYERS': 'LiuG:res1',
                            'format': 'image/png',
                        },
                    maxZoom: 22,
                });

    var layerGetFeature2 = new ol.layer.Tile
        ({
            source: getFeatureSource2,
            layerName: '省会'
        });

    layerGetFeature2.setMaxResolution(10000);

    layerGetFeature2.setMinResolution(100);

    //map.addLayer(layerGetFeature2);
    

    //map.on('singleclick', function (evt)
    //{
    //    var viewResolution = view2.getResolution();
    //    var viewProjection = view2.getProjection();
    //    var url = getFeatureSource2.getGetFeatureInfoUrl(
    //        evt.coordinate, viewResolution, viewProjection, { 'INFO_FORMAT': 'text/html' });
    //    if (url)
    //    {
    //        document.getElementById('info').innerHTML =
    //     '<iframe seamless src="' + url + '"></iframe>';
    //    }
    //});

   
    
    var kmlLayer = new ol.layer.Vector({
        source: new ol.source.KML({
            projection: 'EPSG:3857',
            url: '../data/test.kml'
        })
    });

    kmlLayer.setMaxResolution(10000);
    kmlLayer.setMinResolution(0.1);

    map.addLayer(kmlLayer);



    var displayFeatureInfo = function (pixel)
    {
        var features = [];
        map.forEachFeatureAtPixel(pixel, function (feature, layer)
        {
            features.push(feature);
        });
        if (features.length > 0)
        {
            var info = [];
            var i, ii;
            for (i = 0, ii = features.length; i < ii; ++i)
            {
                info.push(features[i].get('name'));
            }
            document.getElementById('info').innerHTML = info.join(', ') || '(unknown)';
            //map.getTarget().style.cursor = 'pointer';
        } else
        {
            document.getElementById('info').innerHTML = '&nbsp;';
            //map.getTarget().style.cursor = '';
        }
    };

    $(map.getViewport()).on('mousemove', function (evt)
    {
        var pixel = map.getEventPixel(evt.originalEvent);
        displayFeatureInfo(pixel);
    });

    map.on('click', function (evt)
    {
        displayFeatureInfo(evt.pixel);
    });


}


